import { defineStore } from 'pinia'

const editorInitData: any = () => {
  return {
    etCode: '',
    etName: '',
    lcdType: '',
    eslWidth: 400,
    eslHeight: 300,
    x0: 0,
    y0: 0,
    remark: '',
    enabled: false,
    companyId: '',
    departmentId: '',
    image: '',
    blockStatus: '',
    parentID: '',
    blockAddColor: '00',
    pathImg: '',
  }
}

export const useEleStore = defineStore({
  id: 'widgets',
  state: () => ({
    widgets: [] as any, // 组件数据
    currentIndex: 0, // 当前操作组件下表
    editorData: editorInitData(), // 编辑器属性数据
    editorBgImg: '', // 背景图
  }),
  getters: {
    getWidgets: (state): Array<any> => state.widgets, // 获取所有组件
    getCurrentIndex: (state) => state.currentIndex, // 获取当前操作组件下标
    getCurrentWidget: (state) => state.widgets[state.currentIndex], // 获取当前操作组件
    getEditorData: (state) => state.editorData, // 获取当前编辑器属性数据
    getEditorBgImg: (state) => state.editorBgImg, // 获取当前编辑器属背景图
  },
  actions: {
    // 添加组件
    async addWidget(obj: any) {
      this.widgets.push(obj)
      return this.widgets
    },
    // 删除对应下标组件
    async delWidget(index: number) {
      return this.widgets.splice(index, 1)
    },
    // 设置当前操作组件
    async setCurIndex(index: number) {
      this.currentIndex = index
      return this.currentIndex
    },
    // 清空组件
    async clearWidgets() {
      this.widgets.length = 0
    },
    // 设置当前编辑器属性数据
    async setEditorData(data: any) {
      this.editorData = data
    },
    async setWidgets(data: any) {
      this.widgets = data
    },
    // 清空编辑器属性
    async clearEditorData() {
      this.editorData = editorInitData()
    },
    // 设置当前编辑器背景图
    async setEditorBgImg(url: string) {
      this.editorBgImg = url
    },
  },
})
